/*
 * @Author: TC (tengchao@taqu.cn)
 * @Date: 2025-10-21 15:50:43
 * @Last Modified by: TC (tengchao@taqu.cn)
 * @Last Modified time: 2025-10-21 17:01:57
 */

import { app } from "db://assets/FrameWork/App";
import { nsMoleInterface } from "../Const/MoleInterface";
import { nsMoleEnum } from "../Const/MoleEunm";
import { nsMoleShopCfg } from "../Config/MoleShopCfg";

/**
 * 打地鼠背包管理
 */
export class MoleBagManager {
    private _bag: nsMoleInterface.IBag = null;
    public get bag(): nsMoleInterface.IBag {
        return this._bag;
    }

    public init(): void {
        this._bag = app.storageMgr.getJson(nsMoleEnum.EMoleStorageKey.Bag) as nsMoleInterface.IBag;
        if (!this._bag.inited) {
            const cfgs = nsMoleShopCfg.getAllShopCfgs();
            this._bag.props = [];
            for (let i = 0; i < cfgs.length; i++) {
                const cfg = cfgs[i];
                this._bag.props.push({
                    id: cfg.id,
                    num: 0,
                });
            }
            this._bag.inited = true;
            this._save();
        }
    }

    /**
     * 获取道具
     * @param id 道具id
     * @returns 道具
     */
    private _getProp(id: number): nsMoleInterface.IPropItem {
        return this._bag.props.find((prop) => prop.id === id);
    }

    /**
     * 增加道具
     * @param id 道具id
     * @param num 增加数量
     */
    public addProp(id: number, num: number = 1): void {
        const prop = this._getProp(id);
        if (prop) {
            prop.num += num;
        }
        this._save();
    }

    /**
     * 减少道具
     * @param id 道具id
     * @param num 减少数量
     */
    public subProp(id: number, num: number = 1): void {
        const prop = this._getProp(id);
        if (prop) {
            prop.num -= num;
        }
        this._save();
    }

    /**
     * 保存背包
     */
    private _save(): void {
        // app.storageMgr.setJson(nsMoleEnum.EMoleStorageKey.Bag, this._bag);
    }
}
